﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Server
{
    public partial class MyMessage : Form
    {
        public event FormExitEventHandler OnFormExit = null;
        public event SendMessageEventHandler OnSendMessage = null;
        private string ipAddress = null;

        public string IpAddress
        {
            get { return ipAddress; }
            set { ipAddress = value; }
        }
       
        public MyMessage()
        {
            InitializeComponent();
        }

        private void btnSend_Click(object sender, EventArgs e)
        {
            if (this.OnSendMessage != null)
            {
                this.OnSendMessage(this, new MessageEventArgs(this.tbxMessage.Text, this.IpAddress));
            }
            this.lbxListMessages.Items.Add("Tôi: " + this.tbxMessage.Text);
            this.tbxMessage.Text = null;
            this.tbxMessage.Focus();
        }

        public void SetMessage(string message)
        {
            if (this.lbxListMessages.InvokeRequired)
            {
                this.lbxListMessages.BeginInvoke(new MethodInvoker(
                    delegate()
                    {
                        this.SetMessage(message);
                    }));
            }
            else
            {
                this.lbxListMessages.Items.Add(this.ipAddress + ": " + message);
            }
        }

        private void MessageBox_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (this.OnFormExit != null)
            {
                this.OnFormExit(this, new FormExitEvenArgs());
            }
        }

        private void MyMessage_Load(object sender, EventArgs e)
        {

        }
    }
}
